package com.haitaoit.chengquan.module.center.activity.info.update;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.PreferenceUtils;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class UpdateNameActivity extends BaseActivity {
    @ViewInject(R.id.update_et)
    EditText update_et;
    private Intent mIntent;
    private String mCpName;
    private String mConect;
    private String mAddress;
    private String mNick;
    private GeoCoder mSearch;
    private   OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
        public void onGetGeoCodeResult(GeoCodeResult result) {
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {//获取地址
                // 没有检索到结果
               ToastUtils.showToast("亲,您的地址是无效的,请重新填写哦!");
            } else {

                httpGetupdate_address_name();
            }
        }

        @Override
        public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {// 获取经纬度
            if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                //没有找到检索结果
            } else {// 获取反向地理编码结果
                Log.e("tag", "reverseGeoCodeResult1:" + reverseGeoCodeResult.toString());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_update_name);
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        mIntent = getIntent();
        //公司名称
        mCpName = mIntent.getStringExtra("SERVICE_GSNAMELL_TITLE");
        //联系人
        mConect = mIntent.getStringExtra("SERVICE_LXRLL_TITLE");
        //地址
        mAddress = mIntent.getStringExtra("SERVICE_ADDRESSLL_TITLE");

        //需求商改昵称
        mNick = mIntent.getStringExtra("nick");
        if (mNick != null) {
            setTitle_V("编辑昵称");
            update_et.setText(mNick);
        }

        if (mCpName != null) {
            setTitle_V("编辑公司名称");
            update_et.setText(mCpName);
        }
        if (mConect != null) {
            setTitle_V("编辑联系人");
            update_et.setText(mConect);

        }
        if (mAddress != null) {
            setTitle_V("编辑地址");
            update_et.setText(mAddress);
        }


        // 光标在最后
        update_et.setSelection(update_et.getText().toString().length());
    }

    @OnClick({R.id.nick_baocun})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nick_baocun:
                if (hasNetWork()) {
                    //  编辑昵称
                    if (mNick != null) {
                        if (!update_et.getText().toString().equals("")) {

                            httpGetupdate_net_name();
                        } else {
                            ToastUtils.showToast("亲，昵称不能为空！");
                        }
                    }
                    //编辑公司名称
                    if (mCpName!=null){
                        if (!update_et.getText().toString().equals("")) {

                            httpGetupdate_company_name();
                        } else {
                            ToastUtils.showToast("亲，公司名称不能为空！");
                        }

                    }
                    //编辑联系人
                    if (mConect!=null){
                        if (!update_et.getText().toString().equals("")) {

                            httpGetupdate_lianxi_name();
                        } else {
                            ToastUtils.showToast("亲，联系人不能为空！");
                        }

                    }
                    //编辑地址
                    if (mAddress!=null){
                        if (!update_et.getText().toString().equals("")) {
                            mSearch = GeoCoder.newInstance();
                            mSearch.geocode(new GeoCodeOption().city("").address(update_et.getText().toString()));
                            mSearch.setOnGetGeoCodeResultListener(listener);
                        } else {
                            ToastUtils.showToast("亲，地址为空！");
                        }


                    }


                }
                break;
        }
        super.onClick(v);
    }

    private void httpGetupdate_address_name() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("address", update_et.getText().toString()));
        String sign = GetSign.genPackageSign(list);


        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("address", update_et.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, true, Constant.GETEDITADDRESS, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            Intent i = new Intent();
                            i.putExtra("ADDRESS_NAME", update_et.getText().toString());
                            setResult(103, i);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });






    }

    private void httpGetupdate_lianxi_name() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("contact", update_et.getText().toString()));
        String sign = GetSign.genPackageSign(list);
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("contact", update_et.getText().toString());
        params.addQueryStringParameter("sign", sign);
        HttpUtilsSingle.doGet(this, true, Constant.GETEDITCONTACT, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            Intent i = new Intent();
                            i.putExtra("LIANXI_NAME", update_et.getText().toString());
                            setResult(102, i);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });





    }

    private void httpGetupdate_company_name() {

        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("company", update_et.getText().toString()));
        String sign = GetSign.genPackageSign(list);


        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("company", update_et.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, true, Constant.GETEDITFWNAME, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            Intent i = new Intent();
                            i.putExtra("COMPANY_NAME", update_et.getText().toString());
                            setResult(101, i);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });

    }

    private void httpGetupdate_net_name() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("user_id", PreferenceUtils.getPrefString("user_id", "")));
        list.add(new NameValuePairSign("net_name", update_et.getText().toString()));
        String sign = GetSign.genPackageSign(list);


        RequestParams params = new RequestParams();
        params.addQueryStringParameter("user_id", PreferenceUtils.getPrefString("user_id", ""));
        params.addQueryStringParameter("net_name", update_et.getText().toString());
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, true, Constant.Getupdate_net_name, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String jsonString = arg0.result.toString();
                JSONObject json;
                try {
                    json = new JSONObject(jsonString);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            return;
                        case 0:// 成功
                            ToastUtils.showToast(Errmsg);
                            Intent i = new Intent();
                            i.putExtra("name", update_et.getText().toString());
                            setResult(10, i);
                            finish();
                            return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
